package com.herench.bs.transfer;


import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * com.herench.adapter.transfer
 *
 * @author wangwenkai
 * @date 2017/10/26/13:59
 * E-mail：605064334@qq.com
 * 当日挂号支付成功确认
 */
public class DoPrePaymentsTransfer_old extends AbstractTransfer {

    public DoPrePaymentsTransfer_old(String method, Client client, String messageMethod) {
        super(method, client,messageMethod);
    }


    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String idCard = (String) params.get("idNo");
        String patientId = (String) params.get("patientId");
        String tranNo = (String) params.get("tranNo");
        String amount = (String) params.get("amount");
        String tradeNo = (String) params.get("tradeNo");
        String moneyType = (String) params.get("moneyType");
        String streamNo = (String) params.get("streamNo");

        Document tplDoc = getTplDoc(getMethod());
        Element Element = tplDoc.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        Element.addAttribute("PATIENT_ID", patientId).addAttribute("VISIT_ID", "").addAttribute("AMOUNT", amount).addAttribute("PAY_WAY", moneyType).addAttribute("TRANSACT_TYPE", "交款").addAttribute("ID_NO", idCard).addAttribute("WIN_NO","30");

        Element paymentElement = tplDoc.getRootElement().element("CommitData").addElement("Payment").addElement("DataRow");
        paymentElement.addAttribute("CARD_NO", idCard).addAttribute("CARD_TYPE", "03").addAttribute("TRAN_NO", tranNo).addAttribute("STREAM_NO", streamNo).addAttribute("AMOUNT", amount).addAttribute("MONEY_TYPE", moneyType);
        paymentElement.addAttribute("BATCH_CODE", "").addAttribute("AUTHORIZATION_CODE", "").addAttribute("TERMINAL_NUMBER", "").addAttribute("BANK_CODE", "").addAttribute("BANK_NAME", "").addAttribute("REFERENCE", "");
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (Dialect.getSuccessfulState().equals(executeStatus)) {
            Element dataRow = document.getRootElement().element("ReturnData").element("Data").element("DataRow");
            DoPrePaymentsInnerMessage innerMessage = new DoPrePaymentsInnerMessage(dataRow);
            resMap.put("rcptNo", innerMessage.getRcptNo());
            resMap.put("code", executeStatus);
            resMap.put("message", "成功！");
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }

    public class DoPrePaymentsInnerMessage implements InnerMessage {

        /**
         * 合同号
         */
        private String rcptNo;


        public DoPrePaymentsInnerMessage(Element element) {
            this.rcptNo = element.attributeValue("RCPT_NO");
        }

        public String getRcptNo() {
            return rcptNo;
        }

    }

}
